import { ajax } from "../../public/promiseAjax.js";
import * as obj from'../public.js';
import{cookie}from'../../public/cookieFnNew.js';
const o = obj.default
const change = [...o.$$('.change')];
console.log(change)
const text = [...o.$$('.text')];
console.log(text)
const petU = cookie.getCookie('petUserD').split(',')
let userTel;
let u;
const sex = [...o.$$('.sex_z')]
console.log(sex)
petU.forEach(val=>{
  const a = val.split(':');
  if(a[0] === 'tel'){
    userTel = a[1]
  }
})
ajax({
  url:'http://localhost:3000/getUser',
  method:'get',
  data:{
    userTel  
  }
}).then(res=>{
  const userMsg = JSON.parse(res).userMsg[0]
  u = userMsg;
  text[0].value = userMsg.nickName;
  text[1].value = userMsg.username;
  text[2].value = userMsg.password;
  text[3].value = userMsg.tel;
  text[4].value = userMsg.age;
  const d = new Date(userMsg.regTime)
  text[5].value = d.toLocaleDateString();
  const s = userMsg.sex;
  if(s === '男'){
    sex[0].checked = true;
  }else if(s === '女'){
    sex[1].checked = true;
  }else{
    sex[2].checked = true;
  }
  console.log(userMsg)
})

// 点击改变可修改；
const main = o.$('main');
main.onclick= function (eve){
  const e = eve||window.event;
  const target = e.target||e.srcElement;
  if(target.className.indexOf('change') !== -1){
    const inp = target.previousElementSibling;
    inp.type = 'text';
    inp.focus();
    inp.setSelectionRange(0,inp.value.length)
    inp.readOnly = false;      
  }
}
const save = o.$('.save')
save.onclick = function (){
  // text[0].value = userMsg.nickName;
  // text[1].value = userMsg.username;
  // text[2].value = userMsg.password;
  // text[3].value = userMsg.tel;
  // text[4].value = userMsg.age;
  const nickName = text[0].value;
  const password = text[2].value;
  const age = text[4].value;
  let arr = []
  if(nickName !== u.nickName){
    console.log(1)
    ajax({
      url:'http://localhost:3000/changeUser',
      method:'get',
      data:{
        userTel,
        nickName
      }
    }).then(res=>{
      console.log(res)
    })
  }
  if(password !== u.password){
    ajax({
      url:'http://localhost:3000/changeUserPassword',
      method:'get',
      data:{
        userTel,
        password
      }
    }).then(res=>{
      console.log(res)
    })
  }
  if(age !== u.age){
    ajax({
      url:'http://localhost:3000/changeUserAge',
      method:'get',
      data:{
        userTel,
        age
      }
    }).then(res=>{
      console.log(res)
    })
  }

}
